package com.swinw.asumall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class OrtherTask {
    public static ExecutorService executorService = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程启动了");
            int a = 10 / 2;
            System.out.println("任务1线程结束");
            return a;
        }, executorService);

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务二线程启动");
            System.out.println("任务二线程结束");
            return 0;
        }, executorService);

        CompletableFuture<Integer> future3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务3线程启动");
            System.out.println("任务3线程结束");
            return -1;
        }, executorService);
        //只要有一个任务执行成功，就可以执行，返回结果是执行成功任务的结果
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(future1, future2, future3);
        Object o = anyOf.get();
//        //表示需要全部的任务都执行成功才可以通过
//        CompletableFuture<Void> allOf = CompletableFuture.allOf(future1, future2, future3);
//        allOf.get();
    }
}
